/**
 * 
 */
Rsd.define('Rsd.form.MulitiSelectEnum', {
    extend: 'Rsd.form.MulitiSelect',
    xtype: 'mulitiselect-enum',
    /**
     * */
    constructor: function MulitiSelectEnum(config) {
        this.apply(config || {});

        if (Rsd.isString(this.enum)) {
            if (Rsd.xtypes[this.enum]) {
                this.dataSource = Rsd.widget(this.enum).items;
            }
            else {
                this.noData = true;
                this.listeners = {
                    'mouseover': {
                        element: 'dom',
                        fn: function () {
                            if (this.noData && Rsd.isString(this.enum) && Rsd.xtypes[this.enum]) {
                                this.noData = false;
                                this.dataSource = Rsd.widget(this.enum).items;
                            }
                            if (this.noData) {
                                this.warn('类型' + this.enum + '不存在。');
                            }

                        }
                    }
                };
            }
            return;
        }
        if (this.enum instanceof Rsd.data.Enum) {
            this.dataSource = this.enum.items;
            return;
        }
        if (Rsd.isArray(this.enum)) {
            this.dataSource = Rsd.create('Rsd.data.Enum', { items: this.enum }).items;
            return;
        }
        if (Rsd.isObject(this.enum) && !(this.enum instanceof Rsd.data.Enum)) {
            if (!Rsd.classes[this.enum.name]) {
                Rsd.define(this.enum.name, {
                    extend: 'Rsd.data.Enum',
                    xtype: this.enum.name,
                    name: this.enum.name,
                    textMember: this.enum.textMember || 'text',
                    valueMember: this.enum.valueMember || 'code',
                    //colorMember:'color',
                    constructor: function () { },
                    items: this.enum.items
                }
                );
            }
            this.dataSource = Rsd.create(this.enum.name, {}).items;
            return;
        }
    },
    /**
     * 通过位运算 重写getValue
     */
    getValue: function getValue() {
        var list = this.callParent();
        var v = 0;
        list.map((p) => { v |= Number.parseInt(p); });
        //console.log(v);
        return v;
    },
    /**
     * 
     * @param value 通过位运算 重写setValue
     */
    setValue: function setValue(value) {
        var v = value || 0;
        var list = [];
        if (rsd.isArray(value)) {
            list = value;
        }
        else {
            for (var i in this.dataSource) {
                var item = this.dataSource[i];

                if (item.value == 0) {
                    continue;
                }

                if ((v & item.value) == item.value) {
                    list.push(item.value.toString());//父类value 是字符数组
                }
            }
        }

        //console.log(list);
        this.callParent(list);
    },
    /**
     *
     * */
    makeControl: function makeControl(config, row) {
        //debugger;
        
        var _row = row || {};
        var _config = config || {};
        var _editable = _config.editable;
        var _value = _row[_config.dataIndex || _config.dataindex || _config.name];

        //要求 row 对象必须 包含dataIndex 属性
        if (_value == undefined) {
            return document.createTextNode(_config.emptyText || '-');
        }
        //debugger;
        var _enum = _config.enum;
        if (Rsd.isString(_config.enum)) {

            if (!Rsd.xtypes[config.enum]) {

                Rsd.warn('类型：' + _config.enum + '不存在。');
            }
            else {
                _enum = Rsd.widget(_config.enum, {
                    "valueMember": _config["valueMember"],
                    "colorMember": _config["colorMember"],
                    "prefix": _config["prefix"],
                });
            }

        }

        if (Rsd.isArray(_config.enum)) {
            _enum = Rsd.create('Rsd.data.Enum', { items: _config.enum });
        }

        if (Rsd.isObject(_config.enum) && !(_enum instanceof Rsd.data.Enum)) {
            if (Rsd.classes[_config.enum.name] == null) {

                Rsd.define(_enum.name, {
                    extend: 'Rsd.data.Enum',
                    xtype: _enum.name,
                    //textMember,valueMember, colorMember 仅在定义时有效
                    textMember: _enum.textMember || 'text',
                    valueMember: _enum.valueMember || 'code',
                    colorMember: _enum.colorMember || 'color',
                    name: _enum.name,
                    constructor: function () { },
                    items: _enum.items
                }
                );

            }

            _config.enum = _enum = Rsd.create(_enum.name, {});
        }

        if (Rsd.isString(_enum)) {
            return document.createTextNode(_value);
        }
        if (!(_enum instanceof Rsd.data.Enum)) {
            throw new Error('config [enum] value (' + _config.enum.toString() + ') is not Rsd.data.Enum.');
        }

         
        var list = _enum.items;
        var _ctrl = null;

        _ctrl = document.createElement('ul');
        _ctrl.setAttribute("value", _value);
        _ctrl.style.listStyleType = "none";
        _ctrl.style.position = 'relative';
        _ctrl.style.margin = '0px';
        _ctrl.style.padding = '0px';
        _ctrl.style.width = '100%';
        _ctrl.style.height = '100%';
        for (var i in list) {

            var item = list[i];
            if (item.value == 0) {
                continue;
            }
            var _checked = (_value & item.value) == item.value;
            if(_config.showAll !==true &&  _checked===false)
            {
                continue;
            }
            var li = document.createElement('li');
            li.style.lineHeight = '25px';
            if(_config.nobr===true)
            {
                //
            }
            else
            {
                li.style.float = 'left';
                li.style.display = 'block';
            }
            
            li.tag = item;
            li.id = item.value;
            var chk = document.createElement('input');
            chk.type = 'checkbox';
            chk.checked = _checked; 
            chk.style.pointerEvents = 'none';
            if (_editable) {
             
                chk.onclick = function () {
                    //show config.selector 
                    alert("Error:MulitiSelectEnum列表编辑模式未实现");
                };
            } 
            li.appendChild(chk);
            li.appendChild(document.createTextNode(item.text));
           
            _ctrl.appendChild(li);

        }
       

        return [_ctrl];
    }
}, function (type) {
    var _enumGetter = function () {

        return this.__enum;
    };

    var _enumSetter = function (value) {

        this.__enum = value;

    }

    this.defineProperty(type, "enum", _enumGetter, _enumSetter, false);


});